<?php

	function findUser($tag, $value)
	{
		$file = "user.xml";
		$xml = new DOMDocument();
		$xml->formatOutput = true;
		$xml->preserveWhiteSpace = false;
		$xml->load($file) or die("Cannot open file");				
		$users = $xml->getElementsByTagName($tag);
		foreach ($users as $user)
		{
			if($user->nodeValue == $value)
			{
				$data = array();
				$par = $user->parentNode;
				foreach ($par->childNodes as $atribut)
					$data[] = $atribut->nodeValue;			
				$rez[] = new User($data[0], $data[1], $data[2], $data[3], $data[4], $data[5]); 
			}		
		}
		return $rez;			// tabela objektov User
	}

	function userToXML($user)
	{
		$file = "user.xml";
		$xml = new DOMDocument();
		$xml->formatOutput = true;
		$xml->preserveWhiteSpace = false;
		$xml->load($file) or die("Cannot open file");
		$root = $xml->documentElement;
		$uID = $root->childNodes->length;		
		$upo = addElement($xml, $root, "user");
			addAttribute($xml, $upo, "ID", ($uID+1));
			addAttribute($xml, $upo, "uime", $user->uime);
			addAttribute($xml, $upo, "ime", $user->ime);
			addAttribute($xml, $upo, "priimek", $user->priimek);
			addAttribute($xml, $upo, "email", $user->email);
			addAttribute($xml, $upo, "geslo", md5($user->geslo));
		$xml->save($file) or die("Cannot write string to file");
	}

	function getReview($gID, $reviews, $review, $atribut)
	{
		$file = "gadget.xml";
		$xml = new DOMDocument();
		$xml->formatOutput = true;
		$xml->preserveWhiteSpace = false;
		$xml->load($file) or die("Cannot open file");				
		$naprave = $xml->getElementsByTagName("ID");
		foreach ($naprave as $naprava)
		{
			if($naprava->nodeValue == $gID)				// najdemo gadget z gID
			{
				$par = $naprava->parentNode;
				foreach ($par->childNodes as $el)
					if($el->nodeName == $reviews)			// najdemo element (reviews)
						foreach ($el->childNodes as $att)
							if($att->nodeName == $review)			// najdemo atribut (review)
								foreach ($att->childNodes as $subatt)
									if($subatt->nodeName == $atribut)		// atribut ($atribut)	
										$data[] = $subatt->nodeValue;			
			}		
		}
		return $data;			// tabela objektov Gadget
	}
	
	function getAttribute($gID, $element, $atribut)
	{
		$file = "gadget.xml";
		$xml = new DOMDocument();
		$xml->formatOutput = true;
		$xml->preserveWhiteSpace = false;
		$xml->load($file) or die("Cannot open file");				
		$naprave = $xml->getElementsByTagName("ID");
		foreach ($naprave as $naprava)
		{
			if($naprava->nodeValue == $gID)				// najdemo gadget z gID
			{
				$par = $naprava->parentNode;
				foreach ($par->childNodes as $el)
				{
					if($el->nodeName == $element)			// najdemo element (lastnost)
					{
						foreach ($el->childNodes as $att)
						{
							if($att->nodeName == $atribut)	// najdemo atribut (cena)
							{
								$data[] = $att->nodeValue;
							}
						}
					}	
				}					
			}		
		}
		return $data;			// tabela objektov Gadget
	}
	
	function findGadget($tag, $value)
	{
		$file = "gadget.xml";
		$xml = new DOMDocument();
		$xml->formatOutput = true;
		$xml->preserveWhiteSpace = false;
		$xml->load($file) or die("Cannot open file");				
		$naprave = $xml->getElementsByTagName($tag);
		foreach ($naprave as $naprava)
		{
			if($naprava->nodeValue == $value)
			{
				$data = array();
				$par = $naprava->parentNode;
				foreach ($par->childNodes as $atribut)
				{
					$data[] = $atribut->nodeValue;	
				}					
				$rez[] = new Gadget($data[0], $data[1], $data[2], $data[3], $data[4]); 
			}		
		}
		return $rez;			// tabela objektov Gadget
	}
		
	function updateReview($gID, $gpreizkusevalec, $gsttesta, $gocena)
	{
		$file = "gadget.xml";
		$xml = new DOMDocument();
		$xml->formatOutput = true;
		$xml->preserveWhiteSpace = false;
		$xml->load($file) or die("Cannot open file");				
		$naprave = $xml->getElementsByTagName("ID");
		
		
		foreach ($naprave as $naprava)
		{
			if($naprava->nodeValue == $gID)
			{
				$par = $naprava->parentNode;
				
				foreach ($par->childNodes as $child)
				{
					if ($child->nodeName == "reviews")
					{
						$update = true;
						
						$review = addElement($xml, $chils, "review");
							addAttribute($xml, $review, "preizkusevalec", $gpreizkusevalec);
							addAttribute($xml, $review, "sttesta", $gsttesta);
							addAttribute($xml, $review, "ocena", $gocena);
						
						addAttribute($xml, $lastnost, "dimenzije", $gdimenzije);
					}
				}
				if($update == false) 
				{					
					$rev = addElement($xml, $par, "reviews");
					$review = addElement($xml, $rev, "review");
							addAttribute($xml, $review, "preizkusevalec", $gpreizkusevalec);
							addAttribute($xml, $review, "sttesta", $gsttesta);
							addAttribute($xml, $review, "ocena", $gocena);
						
				}
			}		
		}
		$xml->save($file) or die("Cannot write string to file");
	}
	
	function updateImage($gID, $gslika)
	{
		$file = "gadget.xml";
		$xml = new DOMDocument();
		$xml->formatOutput = true;
		$xml->preserveWhiteSpace = false;
		$xml->load($file) or die("Cannot open file");				
		$naprave = $xml->getElementsByTagName("ID");
		
		
		
		foreach ($naprave as $naprava)
		{
			if($naprava->nodeValue == $gID)
			{
				$par = $naprava->parentNode;
				
				foreach ($par->childNodes as $child)
				{
					if ($child->nodeName == "slika")
					{
						$update = true;
						addAttribute($xml, $child, "link", $gslika);
					}
				}
				if($update == false) 
				{					
					$slika = addElement($xml, $par, "slika");
					addAttribute($xml, $slika, "link", $gslika);
				}
			}		
		}
		$xml->save($file) or die("Cannot write string to file");
	}
	
	function updateNapravaLastnosti($gID, $gdimenzije, $gbarva, $gteza, $gcena)
	{
		$file = "gadget.xml";
		$xml = new DOMDocument();
		$xml->formatOutput = true;
		$xml->preserveWhiteSpace = false;
		$xml->load($file) or die("Cannot open file");				
		$naprave = $xml->getElementsByTagName("ID");
		foreach ($naprave as $naprava)
		{
			if($naprava->nodeValue == $gID)
			{
				$par = $naprava->parentNode;
				
				foreach ($par->childNodes as $child)
				{
					if ($child->nodeName == "lastnosti")
					{
						$update = true;
						$target = $child;
					}
				}
				
				if($update == true)
				{
					$fotr = $target->parentNode;
					$fotr->removeChild($target);
					
					$lastnost = addElement($xml, $par, "lastnosti");
			
					addAttribute($xml, $lastnost, "dimenzije", $gdimenzije);
					addAttribute($xml, $lastnost, "barva", $gbarva);
					addAttribute($xml, $lastnost, "gteza", $gteza);
					addAttribute($xml, $lastnost, "cena", $gcena);
				}
				else
				{
					$lastnost = addElement($xml, $par, "lastnosti");
			
					addAttribute($xml, $lastnost, "dimenzije", $gdimenzije);
					addAttribute($xml, $lastnost, "barva", $gbarva);
					addAttribute($xml, $lastnost, "gteza", $gteza);
					addAttribute($xml, $lastnost, "cena", $gcena);
				}
			}		
		}
		$xml->save($file) or die("Cannot write string to file");
	}
	function updateKomentar($gID, $komentator, $kom)
{
    $file = "gadget.xml";
    $xml = new DOMDocument();
    $xml->formatOutput = true;
    $xml->preserveWhiteSpace = false;
    $xml->load($file) or die("Cannot open file");
    $naprave = $xml->getElementsByTagName("ID");

    $update=false;

    foreach ($naprave as $naprava)
    {
        if($naprava->nodeValue == $gID)
        {
            $par = $naprava->parentNode;

            foreach ($par->childNodes as $child)
            {
                if ($child->nodeName == "komentarji")
                {
                    $update = true;

                    $komentar = addElement($xml, $par, "komentar");
                    addAttribute($xml, $komentar, "komentator", $komentator);
                    addAttribute($xml, $komentar, "kome", $kom);

                    	
                }
            }
            if($update == false)
            {
                $par = addElement($xml, $par, "komentarji");
                $komentar = addElement($xml, $par, "komentar");
                addAttribute($xml, $komentar, "komentator", $komentator);
                addAttribute($xml, $komentar, "kome", $kom);
            }
        }
    }
    $xml->save($file) or die("Cannot write string to file");
}

	function napravaToXML($naprava)
	{
		$file = "gadget.xml";
		$xml = new DOMDocument();
		$xml->formatOutput = true;
		$xml->preserveWhiteSpace = false;
		$xml->load($file) or die("Cannot open file");
		$root = $xml->documentElement;
		$gID = $root->childNodes->length;
		$gadget = addElement($xml, $root, "gadget");
			addAttribute($xml, $gadget, "ID", ($gID+1));
			addAttribute($xml, $gadget, "vrsta", $naprava->vrsta);
			addAttribute($xml, $gadget, "proivajalec", $naprava->proizvajalec);
			addAttribute($xml, $gadget, "model", $naprava->model);
			addAttribute($xml, $gadget, "opis", $naprava->opis);
		$xml->save($file) or die("Cannot write string to file");
		
		return $gID;
	}
	
	function addAttribute($xmlDoc, $user, $name, $value)
	{
		$temp = $xmlDoc->createElement($name, $value);
		$temp = $user->appendChild($temp);
	}
	
	function addElement($xmlDoc, $root, $name)
	{
		$el = $xmlDoc->createElement($name);
		$el = $root->appendChild($el);
		return $el;
	}

?>